package com.hzhanghuan.app;

import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hzhanghuan.recyclerview.adapter.ModuleAdapter;

/**
 * Created by Administrator on 2016/6/7.
 */
public class TestModuleAdapter extends ModuleAdapter {


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1) {
            return new IntegerViewHolder(LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false));
        } else if (viewType == 2) {
            return new StringViewHolder(LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false));
        }
        return null;
    }


    @Override
    public int getItemViewType(int position) {
        Module module = getItemManager().findModuleFromPosition(position);
        return module.getType();
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == 1) {
            IntegerViewHolder integerViewHolder = (IntegerViewHolder) holder;
            Module<Integer> module = getItemManager().findModuleFromPosition(position);
            integerViewHolder.textView.setText(module.getItemFromAdapterPosition(position) + "");
        } else if (viewType == 2) {
            StringViewHolder stringViewHolder = (StringViewHolder) holder;
            Module<String> module = getItemManager().findModuleFromPosition(position);
            stringViewHolder.textView.setText(module.getItemFromAdapterPosition(position));

        }


    }


    public class StringViewHolder extends RecyclerView.ViewHolder {


        public TextView textView;


        public StringViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(android.R.id.text1);
            textView.setTextColor(Color.GREEN);
        }
    }

    public class IntegerViewHolder extends RecyclerView.ViewHolder {

        public TextView textView;


        public IntegerViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(android.R.id.text1);
            textView.setTextColor(Color.RED);
        }
    }
}
